package com.ndp.fb.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by yeahmobi on 2015/3/24.
 */
public class LoggerConfigWatcher {
    private static LoggerConfigWatcher configWatcher = new LoggerConfigWatcher();
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(LoggerConfigWatcher.class);

    private LoggerConfigWatcher() {
    }

    public static LoggerConfigWatcher singleton() {
        return configWatcher;
    }

    /**
     * 配置等级
     *
     * @param configMap
     */
    public void resetLog(Map<String, String> configMap) {
        logger.info("调用配置重新定义log>>>>");
        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        for (Map.Entry<String, String> entry : configMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (key.startsWith("logging.level.")) {
                logger.info("配置中心日志key>>>>" + key + ",value>>>" + value);
                String logName = key.substring(14);
                logger.info("配置中心日志 解析出名称logName>>>>" + logName + ",value>>>" + value);
                Logger logger = lc.getLogger(logName);
                logger.setLevel(Level.toLevel(value));
            } else {
                logger.info("配置>>>>" + key + ",value>>>" + value);
            }
        }
    }
}
